# -*- coding: utf-8 -*-
# @Time    : 2024/3/9 15:55
# @Author  : xh
# @FileName: handle_mysql.py
# @Software: PyCharm
import pymysql as pymysql

class HandleDB:
    def __init__(self,host,port,user,password,*args,**kwargs):
        self.con = pymysql.connect(
            host=host,
            port=port,
            user= user,
            password=password,
            charset="utf8",
            #cursorclass=pymysql.cursors.DictCursor #设置游标对象返回的数据类型（字典），默认是元组
        )
    def find_one(self,sql):
        """
        查询一条数据
        """
        with self.con as cur:
            cur.execute(sql)
        res = cur.fetchone()
        cur.close()
        return res
    def find_all(self,sql):
        """
        查询所有数据
        """
        with self.con as cur:
            cur.execute(sql)
        res = cur.fetchhall()
        cur.close()
        return res
    def find_count(self,sql):
        """
        查询所有数据
        """
        with self.con as cur:
            res = cur.execute(sql)
        cur.close()
        return res

    def __del__(self):
        """对象销毁自动执行"""
        self.con.close()

